//
//  LCYLoginViewController.m
//  EPA
//
//  Created by lanou3g on 16/4/26.
//  Copyright © 2016年 liu. All rights reserved.
//

#import "LCYLoginViewController.h"
#import "LCYHomePageController.h"
#import "LCYNetWorkRequestManager.h"
#import "LCYUserInfoManager.h"
#import "LCYRegisterViewController.h"
@interface LCYLoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *emailTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;

@end

@implementation LCYLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(didClickBackBtn:)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(didClickRegisterBtn:)];
    
}
// 返回按钮
- (void)didClickBackBtn:(UIBarButtonItem *)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
/// 注册按钮
- (void)didClickRegisterBtn:(UIBarButtonItem *)sender {
    LCYRegisterViewController *registerVC = [[LCYRegisterViewController alloc]init];
    [self.navigationController pushViewController:registerVC animated:YES];
}
#pragma mark - 登录按钮
- (IBAction)didClickLoginButton:(UIButton *)sender {
    
    [LCYNetWorkRequestManager requestURL:[NSURL URLWithString:@"http://api2.pianke.me/user/login"] RequestMethod:LCY_POST Params:@{@"email":self.emailTextField.text,@"passwd":self.passwordTextField.text} Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSMutableDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *dataDic = jsonDic[@"data"];
            NSString *msg = dataDic[@"msg"];
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:msg?msg:@"登录成功" preferredStyle:UIAlertControllerStyleAlert];
            [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if ([jsonDic[@"result"] isEqual:@1]) {
//                    LCYHomePageController *homePageVC = [[LCYHomePageController alloc]init];
                    LCYUserInfoManager *manager = [LCYUserInfoManager shareManager];
                    NSLog(@"uid:%@, uname:%@, auth：%@",manager.uid,manager.uname,manager.auth);
                    
                
                    
                    [self dismissViewControllerAnimated:YES completion:^{
                        
                    }];
                }
            }]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self presentViewController:alertC animated:YES completion:^{
                    
                }];
            });
            
            if ([jsonDic[@"result"] isEqual:@1]) {
                [[LCYUserInfoManager shareManager] loginWithUserDic:dataDic];
            }
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
